package com.pubinfo.passbook.common.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author zhouch
 * @date 2021/11/9 9:22
 */
public class StreamUtils {
  
  /**
   * 通过对象属性过滤
   * 配合 stream.filter 使用
   *
   * @param executor
   * @param <T>
   * @return
   */
  public static <T> Predicate<T> distinctByKey(Function<T, ?> executor) {
    Map<Object, Boolean> m = new ConcurrentHashMap<>();
    
    return t -> m.putIfAbsent(executor.apply(t), Boolean.TRUE) == null;
  }
  
}
